﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogisticsBLL;
using Model;
using System.Data;

namespace WebLosgic.TruckTeam
{
    public partial class UpdateTruck : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                int id = Convert.ToInt32(Request.QueryString["tid"]);
                if (id != 0) { 
                  BindTruck(id);
                  ddlBind();
                  ddltypebing();
                }
                else
                {
                    Response.Redirect("CLxinxi.aspx");
                }
            }
        }
        public void BindTruck(int id)
        {

            TruckBLL tub = new TruckBLL();
            Model.Trucks mt = tub.BIndturck(id);
            txtNumb.Text = mt.Number;
            RblState.SelectedValue = mt.State.ToString();
            ddlTeamName.SelectedValue=mt.TeamID.ToString(); 
            TxtLength.Text = mt.Length;
            txtTonger.Text = mt.Tonnage;
            txtbuydate.Text = mt.BuyDate.ToString();
            TxtRemark.Text = mt.Remark;
            ddlType.SelectedValue = mt.Type.ToString();
        }
        

        public void ddlBind()
        {
            TruckBLL tru = new TruckBLL();
            DataTable ta = tru.ddlbindCarName();
            ddlTeamName.DataSource = ta;
            ddlTeamName.DataTextField = "TeamName";
            ddlTeamName.DataValueField = "TeamID";
            ddlTeamName.DataBind();
          
        }

        public void ddltypebing()
        {
            TruckBLL tru = new TruckBLL();
            DataTable ta = tru.ddlbindCarType();
            ddlType.DataSource = ta;
            ddlType.DataTextField = "Type";
            ddlType.DataValueField = "Type";
            ddlType.DataBind();
        }



        protected void Btnbian_Click(object sender, EventArgs e)
        {
            TruckBLL tub = new TruckBLL();
            Trucks mt= new Trucks();
            int id = Convert.ToInt32(Request.QueryString["tid"]);
            mt.Number=txtNumb.Text;
            mt.Type = ddlType.SelectedValue;
            mt.State=Convert.ToInt32(RblState.SelectedValue);
             mt.TeamID=Convert.ToInt32(ddlTeamName.SelectedValue);
             mt.Length=TxtLength.Text ;
            mt.Tonnage=txtTonger.Text  ;
            mt.BuyDate=Convert.ToDateTime(txtbuydate.Text);
            mt.Remark=TxtRemark.Text ;
            mt.TruckID = id;
            int resut =tub.updatruck(mt);
            if (resut > 0)
            {
                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 修改车辆信息";
                int TypeId = 10;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters =@""+txtNumb.Text + "," + ddlType.SelectedValue + "," + RblState.SelectedValue + "," + ddlTeamName.SelectedValue;
                string ProcName = "update Truck set Length where TruckID";
                HttpRequest request = HttpContext.Current.Request;
                string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(result)) { result = request.ServerVariables["REMOTE_ADDR"]; }
                if (string.IsNullOrEmpty(result)) { result = request.UserHostAddress; }
                if (string.IsNullOrEmpty(result)) { result = "192.168.0.0"; }
                string IP = "192.168.0.1";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功'); location.href='CLxinxi.aspx'", true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改失败')", true);
                }
                
            }
        }

        protected void BtnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("CLxinxi.aspx");
        }
    }
    
}